import Vue from 'vue'
import Router from 'vue-router'
import index from '@/views/index'
import home from '@/views/home'
import login from '@/views/login'
import course from '@/views/course/courseIndex'
import courseList from '@/views/course/courseList'
import video from '@/views/course/video'
import wrongBook from '@/views/wrongBook/index'
import wrongBookDetail from '@/views/wrongBook/wrongBookDetail'
import wrongBookList from '@/views/wrongBook/wrongBookList'

import questionMode from '@/views/mode/questionMode'
import writingBoard from '@/views/writingBoard'
import exam from '@/views/mode/exam'
import about from '@/views/about'
import cooperation from '@/views/cooperation'
import yelp from '@/views/yelp'
import space from '@/views/space'
import examHistory from '@/views/education/examHistory'
import courseDetail from '@/views/course/courseDetail'
Vue.use(Router)

const router =  new Router({
  routes: [
    {
      path: '/login',
      component: login,
      name: 'login',
    },
    {
      path: '/writingBoard',
      component: writingBoard,
      name: 'writingBoard',
    },
    { path: '/',
      component: index,
      name: 'index',
      redirect: {name: 'home'},
      meta: { title: '主入口整体布局' },
      children: [
        { path: '/home', component: home, name: 'home', meta: { title: '首页' } },
        { path: '/course', component: course, name: 'course', meta: { title: '课程训练' } },
        { path: '/video', component: video, name: 'video', meta: { title: '视频播放' } },
        { path: '/courseList', component: courseList, name: 'courseList', meta: { title: '模式课程列表' } },
        { path: '/wrongBook', component: wrongBook, name: 'wrongBook', meta: { title: '错题本' } },
        { path: '/wrongBookDetail', component: wrongBookDetail, name: 'wrongBookDetail', meta: { title: '错题本详情' } },
        { path: '/wrongBookList', component: wrongBookList, name: 'wrongBookList', meta: { title: '错题本列表' } },
        { path: '/questionMode', component: questionMode, name: 'questionMode', meta: { title: '题库模式' } },
        { path: '/exam', component: exam, name: 'exam', meta: { title: '考试' } },
        { path: '/about', component: about, name: 'about', meta: { title: '关于我们' } },
        { path: '/cooperation', component: cooperation, name: 'cooperation', meta: { title: '校企合作' } },
        { path: '/yelp', component: yelp, name: 'yelp', meta: { title: '智能点评' } },
        { path: '/space', component: space, name: 'space', meta: { title: '空白页面' } },
        { path: '/examHistory', component: examHistory, name: 'examHistory', meta: { title: '空白页面' } },
        { path: '/courseDetail', component: courseDetail, name: 'courseDetail', meta: { title: '空白页面' } },
      ]
    }
  ]
})

// vue  路由全局拦截器
router.beforeEach(function (to, from, next) {
  let token = localStorage.getItem('front-token')
  if (to.path === '/login') {
    if (token) {
      location.href = '/'
    } else {
      next()
    }
  } else {
    if (!token) {
      next({ name: 'login' }) // 没有token 跳转登录页面
    }
    next()
  }
})
export default router
